package com.tarena.lbs.business.producer;


import com.tarena.lbs.business.serializer.MessageSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AmqpProducer<T> {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MessageSerializer<T> messageSerializer;

    public void send(T message, String exchange, String routingKey) {
        log.info("发送消息：{}", message);
        byte[] body = messageSerializer.serialize(message);
        Message msg=new Message(body);
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }
}